package com.example.module_publish;

import android.media.MediaCodec;
import android.media.MediaFormat;
import android.media.MediaMuxer;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.TextureView;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.module_publish.rtmp.Config;
import com.example.module_publish.rtmp.MediaEncoder;
import com.example.module_publish.rtmp.VideoHelper;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;

public class RtmpActivity extends AppCompatActivity  {
    private TextureView textureView;
    private Config config;
    private VideoHelper videoHelper;
    private MediaEncoder mediaEncoder;

    private boolean isPublish;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_rtmp);
        //动态权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{
                    "android.permission.ACCESS_NETWORK_STATE",
                    "android.permission.ACCESS_WIFI_STATE",
                    "android.permission.WRITE_EXTERNAL_STORAGE",
                    "android.permission.READ_EXTERNAL_STORAGE",
                    "android.permission.RECORD_AUDIO",
                    "android.permission.MODIFY_AUDIO_SETTINGS",
                    "android.permission.BLUETOOTH",
                    "android.permission.CAMERA",
                    "android.permission.READ_PHONE_STATE",
            }, 101);
        }
        textureView = findViewById(R.id.view);

        config = new Config.Builder()
                .setUrl("rtmp://192.168.1.105:1935/live/test")
                .setFps(30)
                .setMinWidth(640)
                .setMaxWidth(1280)
                .setBitrate(2 * 1024 * 1024)
                .build();
        videoHelper = VideoHelper.newInstance(config);
        mediaEncoder = MediaEncoder.newInstance(config);
        //2222222:预览画面返回原始数据YUV 420P
        videoHelper.setCallback(new VideoHelper.Callback() {
            @Override
            public void onReceive(byte[] data) {
                // 4444444:将数据给到编码器
                Log.d("RtmpActivity", "采集到原始数据YUV: "+data.toString());
                if (isPublish){
                    mediaEncoder.putVideoData(data);
                    Log.d("RtmpActivity", "采集到原始数据喂给编码器: "+data.toString());
                }
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        //1111:画面预览
        videoHelper.initCamera(textureView.getSurfaceTexture());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        videoHelper.release();
        mediaEncoder.releaseVideoEncoder();
    }
    //333333:开始录制编码
    public void startPush(View view) throws IOException {
        isPublish = true;
        mediaEncoder.initVideoEncoder(videoHelper.previewSize.width,videoHelper.previewSize.height); //初始化编码器
        mediaEncoder.startVideoEncode();
        // 5555555: 回调编码后的数据
        mediaEncoder.setCallback(new MediaEncoder.Callback() {
            @Override
            public void outputVideoData(ByteBuffer bb, MediaCodec.BufferInfo info) {
                //66666:编码后的数据 1:封装    2:rtmp推流协议
                Log.d("RtmpActivity", "编码后的数据bb: "+bb.toString());
                Log.d("RtmpActivity", "编码后的数据info: "+info.toString());
            }

            @Override
            public void outputAudioData(ByteBuffer bb, MediaCodec.BufferInfo info) {

            }
        });
    }

    public void endPush(View view) {
        isPublish = false;
        mediaEncoder.stopVideoEncode();
    }
}